package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.CheckGroup;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {


    @Reference
    private CheckGroupService checkGroupService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            //调用业务层方法
            return checkGroupService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //添加
    @RequestMapping("/add")
    public Result addCheckGroup(Integer[] checkItemIds, @RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.addCheckGroup(checkItemIds, checkGroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    //删除
    @RequestMapping("/delete")
    public Result deleteCheckGroup(Integer checkGroupId) {

        /*System.out.println(id);
        return new Result(true,"hh");*/

        try {
            checkGroupService.deleteCheckGroup(checkGroupId);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    //编辑数据回显
    @RequestMapping("/findByCheckGroupId")
    public Result findCheckGroupInfo(Integer checkGroupId) {
        //System.out.println(checkGroupId);

        try {
            CheckGroup checkGroupInfo = checkGroupService.findCheckGroupInfo(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //查询与检查组绑定的检查项的id
    @RequestMapping("/findAllCheckItemByGid")
    public Result findAllCheckItemByGid(Integer checkGroupId) {
        try {
            List<Integer> checkItemIds = checkGroupService.findAllCheckItemByGid(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //更新数据
    @RequestMapping("/edit")
    public Result edit(Integer[] checkItemIds, @RequestBody CheckGroup checkGroup) {
        /*
        检查从前台传过来的数据
        for (Integer checkItemId : checkItemIds) {
            System.out.println(checkItemId);
        }
        System.out.println("==================");
        System.out.println(checkGroup);
        */

        try {
            checkGroupService.edit(checkItemIds, checkGroup);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //查询所有检查组
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> checkGroups = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
